package com.zego.wawaji.ui.center.wawa;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.noble.library.basic.log.PLogger;
import com.zego.wawaji.base.module.ModuleMgr;
import com.noble.library.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.noble.library.basic.string.StringUtils;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.ui.base.ExBaseAdapter;
import com.zego.wawaji.ui.bean.wawa.MyWawa;
import com.zego.wawaji.ui.bean.wawa.Wawa;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的娃娃：寄存中
 * Created by Su on 2017/11/26.
 */
public class UntreatedAdapter extends ExBaseAdapter<MyWawa> {
    private List<MyWawa> chooseList = new ArrayList<>();   // 选中的奖品
    private List<MyWawa> bigWawaList = new ArrayList<>();

    private int refreshType = 0;        // 0: 默认常规刷新  1： 全选刷新  2： 取消全选刷新

    public UntreatedAdapter(Context context, List<MyWawa> datas) {
        super(context, datas);
    }

    public void setRefreshType(int refreshType) {
        this.refreshType = refreshType;
        reset();
        if (refreshType == 1) { // 全选
            chooseList.addAll(getList());
            for (int i = 0; i < getList().size(); i++) {
                getList().get(i).setChoose(true);
                Wawa wawa = ModuleMgr.getWawaMgr().getWawa(getList().get(i).getWwTypeId());
                if (wawa.isBig()) {
                    bigWawaList.add(getList().get(i));
                }
            }
        }
        if (refreshType == 2) {
            for (int i = 0; i < getList().size(); i++) {
                getList().get(i).setChoose(false);
            }
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final MyViewHolder holder;
        if (convertView == null) {
            convertView = inflate(R.layout.my_wawa_untreated_item);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        final MyWawa info = getItem(position);
        final Wawa wawa = ModuleMgr.getWawaMgr().getWawa(info.getWwTypeId());

        ImageLoader.loadRoundAvatar(getContext(), wawa.getImg(), holder.wawa_icon);
        holder.wawa_name.setText(wawa.getName());
        holder.time_info.setText(StringUtils.getCreateTime(info.getCreateTime()));
        holder.exchangeNum.setText("可兑" + wawa.getExchange() + "娃娃币");
        holder.time_left.setText(callBackTime(info));

        holder.iv_choose.setSelected(info.isChoose());
        holder.rl_item.setOnClickListener(v -> {
            try {
                if (refreshType != 0) { // 全选状态
                    MsgMgr.getInstance().sendMsg(MsgType.MT_Cancel_Choose, refreshType);
                }
                refreshType = 0;
                if (holder.iv_choose.isSelected()) {
                    holder.iv_choose.setSelected(false);
                    if (chooseList.contains(info)) {
                        chooseList.remove(info);
                    }
                    if (bigWawaList.contains(info)) {
                        bigWawaList.remove(info);
                    }
                    info.setChoose(false);
                } else {
                    holder.iv_choose.setSelected(true);
                    chooseList.add(info);
                    if (wawa.isBig()) {
                        bigWawaList.add(info);
                    }
                    info.setChoose(true);
                }
            } catch (Exception e) {
                PLogger.printThrowable(e);
            }
        });
        return convertView;
    }

    private String callBackTime(MyWawa info) {
        long currentTime = ModuleMgr.getAppMgr().getTime();
        long distance = info.getCallbackTime() - currentTime / 1000;
        String ret = "";
        // 刷新列表
        if (distance <= 0) {
            try {
                getList().remove(info);
                notifyDataSetChanged();
            } catch (Exception e) {
            }
        } else if (distance < 60) {
            ret = "寄存剩余" + distance + "秒";
        } else if (distance < 60 * 60) {
            distance = distance / 60;
            ret = "寄存剩余" + distance + "分钟";
        } else if (distance < 60 * 60 * 24) {
            distance = distance / (60 * 60);
            ret = "寄存剩余" + distance + "小时";
        } else {
            distance = distance / (60 * 60 * 24);
            ret = "寄存剩余" + distance + "天";
        }
        return ret;
    }

    private void reqExchange(MyWawa info) {
        ModuleMgr.getWawaMgr().reqExchangeWawa(info.getId(), response -> {
            if (response.isOk()) {
                MsgMgr.getInstance().sendMsg(MsgType.MT_Wawa_Untreated, null);
                MsgMgr.getInstance().sendMsg(MsgType.MT_Update_MyInfo, null);
                Exchange exchange = (Exchange) response.getBaseData();
                if (exchange != null) {
                    PickerDialogUtil.showWawaExchangeDialog((FragmentActivity) getContext(), exchange.getExchangeNum());
                }
            }
        });
    }

    public List<MyWawa> getChooseList() {
        return chooseList;
    }

    public List<MyWawa> getBigWawaList() {
        return bigWawaList;
    }

    public void reset() {
        if (chooseList != null)
            chooseList.clear();
        if (bigWawaList != null)
            bigWawaList.clear();
    }

    private class MyViewHolder {
        private ImageView wawa_icon, iv_choose;
        private TextView wawa_name, time_info, exchangeNum, time_left;
        private RelativeLayout rl_item;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            wawa_icon = (ImageView) convertView.findViewById(R.id.wawa_icon);
            wawa_name = (TextView) convertView.findViewById(R.id.wawa_name);
            time_info = (TextView) convertView.findViewById(R.id.time_info);
            exchangeNum = (TextView) convertView.findViewById(R.id.exchange_num);
            time_left = (TextView) convertView.findViewById(R.id.time_left);
            iv_choose = (ImageView) convertView.findViewById(R.id.iv_choose);
            rl_item = (RelativeLayout) convertView.findViewById(R.id.rl_item);
        }
    }
}
